﻿
CREATE PROCEDURE [dbo].[proc_Finance_RunningAccount_UpdateOrderGreen]
	(
		@OId uniqueidentifier
	)
AS
--By GSJ 2012-08-14 将16:30后的数据调整到第2天 Begin--
	Declare @nDays int,@nHour int,@nMinute int,@dToday DateTime
--	Set @dToday = GETDATE() --初始化日期
	--16:30分调整日期
--	Set @nHour = 16
--	Set @nMinute = 30

	--今天+1天与下月1日进行比较，相同则说明今天是本月最后1天
--	If Month(GetDate())<12
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())) + '-' + Convert(varchar(2),Month(GetDate())+1) + '-1'))
--	Else
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())+1) + '-01-01'))
--	If @nDays>0 --不是最后1天
--	Begin
--		If DatePart(hh,GETDATE())>@nHour Or (DatePart(hh,GETDATE())=@nHour And DatePart(mi,GETDATE())>@nMinute)	--超过16:30分
--			Set @dToday = DateAdd(hh,12,GETDATE())	--加12小时
--	END
	Set @dToday=dbo.Get_SetDateTime()
--By GSJ 2012-08-14 将16:30后的数据调整到第2天 End --

	Declare @TrackKfDepId int,@TrackKfName nvarchar(30),@TrackKfAccount varchar(30),@TrackBDName nvarchar(30),@TrackBDAccount varchar(30)
	--初始化 跟进客服的部门编号、姓名、账号
	Set @TrackKfDepId=0
	Set @TrackKfName=''
	Set @TrackKfAccount=''
	set @TrackBDName=''
	Set @TrackBDAccount=''

	select @TrackKfDepId=isnull((select departmentid from employee where employee.username=customer.ServicePerson),0),@TrackKfName=isnull(ServicePersonEName,''),@TrackKfAccount=isnull(ServicePerson,'')
			,@TrackBDName=isnull(BDServicePersonEName,''),@TrackBDAccount=isnull(BDServicePerson,'') from customer
		where customerid =(select cusid from [order] where oid=@OId)

--
	Update [Order] Set StateId=2,IsGreen=1,FinanceRenlingDate=@dToday,flag=1,
	TrackKfDepId=@TrackKfDepId,TrackKfName=@TrackKfName,TrackKfAccount=@TrackKfAccount
		,TrackBDAccount=@TrackBDAccount,TrackBDName=@TrackBDName
	 Where OId=@OId
	RETURN